package kt.com.bilin.coin.ui.fragment

import android.os.Bundle
import android.view.View
import com.bilin.coin.R
import com.bilin.coin.bean.CommonSuccessEvent
import com.bilin.coin.network.RetrofitConfig
import kotlinx.android.synthetic.main.fragment_v2_task.*
import kt.com.bilin.coin.ad.AdManager
import kt.com.bilin.coin.api.API_SERVICE
import kt.com.bilin.coin.api.ApiObserver
import kt.com.bilin.coin.api.toData
import kt.com.bilin.coin.bean.CZTaskBean
import kt.com.bilin.coin.common.ARouterNavigation
import kt.com.bilin.coin.common.bindViewClick
import kt.com.bilin.coin.common.toastMessageLong
import kt.com.bilin.coin.ui.dialog.WxReadDialog
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class TaskFragment : BaseFragment(R.layout.fragment_v2_task) {
    private var typeAddTime = "0" //0比邻券 1种子 2工分
    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        bindViewClick(this, bt1, bt2, bt3, bt3_1, bt3_2, bt3_3, bt3_4, tvTaskDesc)
        refreshLayout.setEnableLoadMore(false)
        refreshLayout.setOnRefreshListener { onReload() }
        onReload()
    }

    private fun onReload() {
        launchUI({ API_SERVICE.getData().toData() }, ApiObserver(context, refreshLayout = refreshLayout) { handData(it) })
    }

    private fun handData(czTaskBean: CZTaskBean?) {
        czTaskBean?.run {
            bt3.isEnabled = !isAdOver
            bt3.text = if (!isAdOver) "去完成" else "已完成"

            bt3_1.isEnabled = !isAdOverTwo
            bt3_1.text = if (!isAdOverTwo) "去完成" else "已完成"

            bt3_2.isEnabled = !isAdOverThree
            bt3_2.text = if (!isAdOverThree) "去完成" else "已完成"
            if (hasKyc) {
                tv11.text = "完成1/1"
                bt1.isEnabled = false
                bt1.text = "已实名"
            } else {
                tv11.text = "完成0/1"
                bt1.isEnabled = true
                bt1.text = "去实名"
            }
            tv111.text = kycAwardStr1
            tv1111.text = kycAwardStr2

            tv22.text = "累计邀请：$directChildKycNum"
            tv222.text = childKycAwardStr1
            tv2222.text = childKycAwardStr2

            tv33.text = "累计完成$times/$perDayTimes"
            tv333.text = adAwardStr1
            tv3333.text = adAwardStr2

            tv33_1.text = "累计完成$timesTwo/$perDayTimesTwo"
            tv333_1.text = adAwardStr1Two
            tv3333_1.text = adAwardStr2Two

            tv33_2.text = "累计完成$timesThree/$perDayTimesThree"
            tv333_2.text = adAwardStr1Three
            tv3333_2.text = adAwardStr2Three

            tv33_3.text = "累计完成$timesFour/$perDayTimesFour"
            tv333_3.text = adAwardStr1Four
            tv3333_3.text = adAwardStr2Four

            tv33_4.text = "累计完成$timesFive/$perDayTimesFive"
            tv333_4.text = adAwardStr1Five
            tv3333_4.text = adAwardStr2Five
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            bt1 -> EventBus.getDefault().post(CommonSuccessEvent(CommonSuccessEvent.GOTOREAL))
            bt2 -> EventBus.getDefault().post(CommonSuccessEvent(CommonSuccessEvent.GOTOINVITE))
            bt3 -> initAndPlayAd("0")
            bt3_1 -> initAndPlayAd("1")
            bt3_2 -> initAndPlayAd("2")
            bt3_3 -> initAndPlayAd("3")
            tvTaskDesc -> ARouterNavigation("/common/WebPageLoading") {
                withString("url", RetrofitConfig.newsUrl + 5)
                withString("title", "任务说明")
            }
            bt3_4 -> launchUI({ API_SERVICE.readAppConf().toData() }, ApiObserver(activity) {
                if (it.isNullOrEmpty()) {
                    "无法阅读".toastMessageLong()
                } else {
                    WxReadDialog(requireActivity(), it).show()
                }
            })
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun loginSuccessEvent(commonSuccessEvent: CommonSuccessEvent) {
        if (commonSuccessEvent.type == CommonSuccessEvent.LoginSuccess) {
            onReload()
        }
        EventBus.getDefault().removeStickyEvent(commonSuccessEvent)
    }

    private fun initAndPlayAd(type: String) {
        typeAddTime = type
        AdManager.loadRewardVideoAd(activity) {
            launchUI({
                API_SERVICE.addTimes(typeAddTime).toData()
                handData(API_SERVICE.getData().toData())
            }, ApiObserver(context))
        }
    }
}